﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using RainEngine.Controllers;
using RainEngine.SceneManagement;

namespace RainEngine.SceneNodes.UI
{
    /// <summary>
    /// 水平滚动条
    /// </summary>
    public class UIHScrollBar:UIScrollBarBase
    {
        #region 成员变量和构造函数

        //默认值        
        private static readonly Rectangle DefaultButtonRectOrigin = new Rectangle(0, 0, 16, 16);
        private static readonly Rectangle DefaultButtonRectHighlight = new Rectangle(16, 0, 16, 16);
        private static readonly Rectangle DefaultButtonRectClicked = new Rectangle(32, 0, 16, 16);
        /// <summary>
        /// 默认水平滚动条大小为232*16。
        /// </summary>
        private static readonly Vector2 DefaultHSize = new Vector2(232, 16);
        
        /// <summary>
        /// 创建一个默认水平滚动条。
        /// </summary>
        /// <param name="engine">引擎</param>
        /// <param name="setScene">所属场景</param>        
        public UIHScrollBar(RainGE engine, Scene setScene)
            : base(engine, setScene,DefaultHSize ,Vector2.Zero, "UIScrollBar", DefaultButtonRectOrigin, DefaultButtonRectHighlight, DefaultButtonRectClicked,DefaultSliderRailRect, DefaultSliderButtonRectOrigin, DefaultSliderButtonRectHighlight, DefaultSliderButtonRectClicked, DefaultSliderButtonGlyphRect,true) 
        {
            
            //添加向左按钮
            btnLeftUp = new UIButton(engine, setScene, btnLeftUpPositon, TextureName, new Vector2 (buttonWidth,buttonWidth ), null, engine.DefaultFont, Color.White, Color.White, buttonRectOrigin, buttonRectHighlight, buttonRectClicked,true);
            btnLeftUp.TabStop = false;
            btnLeftUp.MouseClick += new System.Windows.Forms.MouseEventHandler(btnUpLeft_MouseClick);
            Scene.AddNode(btnLeftUp);
            
            //添加向右按钮
            btnRightDown = new UIButton(engine, setScene, btnRightDownPosition, TextureName, new Vector2(buttonWidth, buttonWidth), null, engine.DefaultFont, Color.White, Color.White, ButtonRectOrigin, ButtonRectHighlight, ButtonRectClicked,true);
            //将向右按钮水平翻转
            btnRightDown.SpriteEffect = SpriteEffects.FlipHorizontally ;
            btnRightDown.TabStop = false;
            btnRightDown.MouseClick += new System.Windows.Forms.MouseEventHandler(btnDownRight_MouseClick);
            Scene.AddNode(btnRightDown);
            
            //绘制水平滚动条
            Redraw();
        }

        #endregion

        public override void Update(GameTime gameTime)
        {

            //设置滑动条上按钮的位置，计算滑动条的值并引发ValueChanged事件           
            if (isMouseInside && Input.MouseLeftButtonPressed)
            {
                isScrolling = true;

                //根据鼠标移动改变滑块的水平位置
                sliderButtonPosition.X = Position.X + buttonWidth;
                sliderButtonPosition.X += Input.MousePos.X - Position.X - buttonWidth - sliderButtonSizeX / 2;

                //不让滑块超出滑动条边界
                if (sliderButtonPosition.X < Position.X + buttonWidth)
                    sliderButtonPosition.X = Position.X + buttonWidth;
                else if (sliderButtonPosition.X > Position.X + buttonWidth + sliderRailSizeX - sliderButtonSizeX)
                    sliderButtonPosition.X = Position.X + buttonWidth + sliderRailSizeX - sliderButtonSizeX;

                int value = 0;
                value = (int)System.Math.Round((Input.MousePos.X - Position.X - buttonWidth - sliderButtonSizeX / 2) * maxStep / (sliderRailSizeX - sliderButtonSizeX));

                //不让value的值超出范围
                if (value < 0)
                    value = 0;
                else if (value > maxStep)
                    value = maxStep;

                //如果值变化则重新设置新值并引发ValueChanged事件
                if (this.value != value)
                {
                    this.value = value;
                    OnValueChanged();
                }

                else
                    isScrolling = false;

                base.Update(gameTime);
            }
        }

        /// <summary>
        /// 绘制滚动条
        /// </summary>
        public override int Draw(GameTime gameTime)
        {

            //获取图像淡入淡出的透明颜色
            alphaTextureColor = new Color(Color.R,Color.G,Color.B, Scene.TransitionAlpha);

            //绘制滑动条轨道
            RainGE.SpriteBatch.Draw(texture, new Rectangle((int)(sliderRailPosition.X), (int)(sliderRailPosition.Y), (int)sliderRailSizeX, (int)buttonWidth), sliderRailRect, alphaTextureColor, Rotation, Origin, SpriteEffect, LayerDepth);

            //根据鼠标移入还是按下设置滑动条按钮图像源矩阵
            Rectangle buttonRect = isMouseInside || (uiManager.ActiveControl == this) ? ((Input.MouseLeftButtonPressed && isMouseInside) ? sliderButtonRectClicked : sliderButtonRectHighlight) : sliderButtonRectOrigin;

            //绘制滑动条上的按钮
            RainGE.SpriteBatch.Draw(texture, new Rectangle((int)sliderButtonPosition.X, (int)Position.Y, (int)sliderButtonSizeX, (int)buttonWidth), buttonRect, alphaTextureColor, Rotation, Origin, SpriteEffect, LayerDepth);

            //绘制滑块上的摩擦条，如果滑块长度小于滚动条高度则无需绘制摩擦条
            if (sliderButtonSizeX > size.Y)
                RainGE.SpriteBatch.Draw(texture, new Rectangle((int)(sliderButtonPosition.X + sliderButtonSizeX / 2 - buttonWidth / 2), (int)sliderButtonPosition.Y, (int)buttonWidth, (int)buttonWidth), sliderButtonGlyphRect, Color, Rotation, Origin, SpriteEffect, LayerDepth);

            return 1;
        }

        #region 事件相关处理程序

        /// <summary>
        /// 重新绘制水平滚动条
        /// </summary>
        protected override  void Redraw()
        {
            //按钮高(宽)度即滚动条的高度
            this.buttonWidth = size.Y; 
            
            //重新设置轨道的位置和大小
            sliderRailPosition = new Vector2(Position.X + buttonWidth, Position.Y);
            //滑动条宽度即滚动条宽度减去2倍按钮高度
            this.sliderRailSizeX = size.X - 2 * buttonWidth; 
            
            //重新设置滑块的位置和大小            
            ResetSliderButtonPosition();
            //滑块宽度由largeChange，sliderRailSizeX和maxStep计算得出
            this.sliderButtonSizeX = largeChange * sliderRailSizeX / (largeChange + maxStep);
            
            //重新设置左右按钮位置和大小
            btnLeftUp.Position=btnLeftUpPositon = Position;            
            btnRightDown.Position=btnRightDownPosition = Position + new Vector2(size.X- buttonWidth, 0);            
            btnLeftUp.Size = btnRightDown.Size = new Vector2 (buttonWidth,buttonWidth );            
        }       

        protected override void ResetSliderButtonPosition()
        {
            if (!isScrolling)
            {
                //根据value值重置按钮的位置
                sliderButtonPosition.X = Position.X+buttonWidth+value *(sliderRailSizeX-sliderButtonSizeX ) / maxStep;
                sliderButtonPosition.Y = Position.Y;
                
                //不让滑块超出滑动条边界
                if (sliderButtonPosition.X < Position.X + buttonWidth)
                    sliderButtonPosition.X = Position.X + buttonWidth;
                else if (sliderButtonPosition.X > Position.X + buttonWidth + sliderRailSizeX - sliderButtonSizeX)
                    sliderButtonPosition.X = Position.X + buttonWidth + sliderRailSizeX - sliderButtonSizeX;
            }
        }        

        #endregion

        
    }
}